package com.zmn.oms.zmn.business.impl.work;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.business.interfaces.mq.MqProviderService;
import com.zmn.oms.business.interfaces.orderstock.OrderStockBService;
import com.zmn.oms.business.interfaces.remark.OrderRemarkBService;
import com.zmn.oms.common.annotation.ZsNormalOrderWorkLog;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderDutyConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.orderstock.ReleaseStockDTO;
import com.zmn.oms.model.dto.remark.OrderRemarkDTO;
import com.zmn.oms.model.dto.work.modify.OrderWorkSuspendDTO;
import com.zmn.oms.model.entity.masterworking.MasterWorking;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.masterworking.MasterWorkingService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkSuspendBService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：工单分单 service
 *
 * @author liuying
 * @date 2019/05/08 18:54
 */
@Service
public class OrderWorkSuspendBServiceImpl implements OrderWorkSuspendBService {

    @Autowired
    private OrderWorkService orderWorkService;
    @Autowired
    private OrderRemarkBService orderRemarkBService;
    @Autowired
    private MasterWorkingService masterWorkingService;
    @Autowired
    private OrderStockBService orderStockBService;

   // @Reference(version = com.zmn.base.engineer.common.constants.DubboConsts.INTERFACE_VERSION, check = false)
    //private EngineerTouchModifyRemoteService engineerTouchModifyRemoteService;

    @Autowired
    private MqProviderService mqProviderService;

    @Transactional(rollbackFor = Exception.class)
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_ORDER_SUSPEND)
    @Override
    public void operateSuspend(OrderWorkSuspendDTO orderWorkSuspendDTO) throws OmsBaseException {

        // 查询订单
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderWorkSuspendDTO.getOrderId(), orderWorkSuspendDTO.getWorkId());

        // 设置挂起状态
        OrderWork updateOrderWork = new OrderWork();
        updateOrderWork.setOrderId(orderWorkSuspendDTO.getOrderId());
        updateOrderWork.setWorkId(orderWorkSuspendDTO.getWorkId());
        updateOrderWork.setNextContactTime(orderWorkSuspendDTO.getNextContactTime());
        updateOrderWork.setDutyStatus(OrderDutyConsts.DUTY_STATUS_SUSPEND);
        updateOrderWork.setDutyFailCode(orderWorkSuspendDTO.getDutyFailCode());
        updateOrderWork.setSuspendTime(new Date());
        // 设置师傅为未出发
        updateOrderWork.setIsLeave(GlobalConsts.NO);

        boolean statusBoolean = Objects.equals(orderWork.getStatus(), OrderStatusConsts.WORK_STATUS_VISIT);
        boolean reStatusBoolean = Objects.equals(orderWork.getResultStatus(), OrderStatusConsts.WORK_RESULT_DOING);
        if (statusBoolean && reStatusBoolean) {
            // 已上门->已领单
            updateOrderWork.setStatus(OrderStatusConsts.WORK_STATUS_TAKE);
            orderWorkSuspendDTO.setStatus(OrderStatusConsts.WORK_STATUS_TAKE);
        }
        orderWorkService.updateOrderWorkSuspendByKey(updateOrderWork);

        List<Integer> supportStatus = Lists.newArrayList(
                OrderStatusConsts.WORK_STATUS_DISTRIBUTE,
                OrderStatusConsts.WORK_STATUS_TAKE,
                OrderStatusConsts.WORK_STATUS_VISIT);
        if (supportStatus.contains(orderWork.getStatus())) {
            MasterWorking masterWorking = new MasterWorking();
            masterWorking.setWorkId(orderWorkSuspendDTO.getWorkId());
            masterWorkingService.updateWorkingDutyTimeByKey(masterWorking);
        }

        if (statusBoolean && reStatusBoolean) {
            // 修改工程师领单状态
            masterWorkingService.updateMasterWorkingStatus(orderWorkSuspendDTO.getWorkId(), OrderStatusConsts.ORDER_STATUS_TAKE);
        }

        // 添加挂起原因
        OrderRemarkDTO orderRemarkDTO = new OrderRemarkDTO();
        orderRemarkDTO.setOrderId(orderWorkSuspendDTO.getOrderId());
        orderRemarkDTO.setWorkId(orderWorkSuspendDTO.getWorkId());
        orderRemarkDTO.setContent(orderWorkSuspendDTO.getReasonDesc());
        orderRemarkDTO.setOperatorId(orderWorkSuspendDTO.getOperatorId());
        orderRemarkBService.insertOrderRemark(orderRemarkDTO, OrderConsts.ORDER_REMARK_TYPE_SUSPEND);

        // 刷新进行中工单对应工程师的工单版本号
        //engineerTouchModifyRemoteService.saveEngineerWorkVersion(orderWork.getMasterId(), null);
        mqProviderService.sendEngineerWorkVersion(orderWork.getMasterId(), null);

        // 释放库存
        ReleaseStockDTO releaseStockDTO = new ReleaseStockDTO();
        releaseStockDTO.setOrderId(orderWork.getOrderId());
        releaseStockDTO.setWorkId(orderWork.getWorkId());
        releaseStockDTO.setMasterId(orderWork.getMasterId());
        releaseStockDTO.setCityId(orderWork.getCityId());
        orderStockBService.releaseStock(releaseStockDTO);
    }
}
